package com.cskaoyan.controller;

import com.cskaoyan.aspectj.admin.AdminLogin;
import com.cskaoyan.aspectj.admin.AdminLogout;
import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.service.AuthService;
import com.cskaoyan.utils.HttpUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {


    @Autowired
    AuthService authService;

    /**
     * Shiro安全框架 → 视频课程 4个课时 → 认证（登录）
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */
    @AdminLogin
    @PostMapping("login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");

        // 获取subject
        Subject subject = SecurityUtils.getSubject();
        //执行登录认证
        MarketToken usernamePasswordToken = new MarketToken(username,password,"admin");
        try {
            subject.login(usernamePasswordToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        //获取管理员的全部信息
        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipal();
        //修改管理员最后登录ip
        marketAdmin.setLastLoginIp(HttpUtil.getIp());
        //修改管理员最后登录时间
        marketAdmin.setLastLoginTime(new Date());
        authService.update(marketAdmin);
        //封装返回值
        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar(marketAdmin.getAvatar());
        adminInfo.setNickName(marketAdmin.getUsername());
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken((String) subject.getSession().getId());
        return BaseRespVo.ok(loginUserData,"成功");
    }

    @RequestMapping("info")
    public BaseRespVo info(String token) {
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //获取管理员
        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipal();
        //查询
        InfoData infoData = authService.info(marketAdmin);
        //封装返回值
        infoData.setName(marketAdmin.getUsername());
        infoData.setAvatar(marketAdmin.getAvatar());
        return BaseRespVo.ok(infoData,"成功");
    }

    @AdminLogout
    @RequestMapping("logout")
    public BaseRespVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok("成功");
    }
}
